/**
 * zmn文章模块-文章列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate','zmnExportUtil'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var zmnExportUtil =layui.zmnExportUtil;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-140',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        },
    });
    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    zmnProduct.laySelectSpecial(form, "/extension/serv/category/group/listChilds.action", "extensionServCategId", "categId");
    var ids = [];

    // 按钮事件
    table.on("toolbar(zmn-table)", function (obj) {
        let items = obj.config.data;
        // ids = zmnPage.layIdsChecked(items, "groupId");

        var ids = new Array();
        for (var k = 0; k < items.length; k++) {
            var o = items[k];
            var isChecked = o["LAY_CHECKED"];
            if (isChecked) {
                ids.push(o['extensionServCategId']+'-'+o['categId']);
            }
        }

        if (obj.event === "delete") {
            if (ids.length === 0) {
                return layer.alert("请选择至少一列！");
            }
            layer.confirm('您确定要删除关联吗？', {
                btn: ['确定', '取消']
            }, function () {
                zmnPage.ajaxSimple("/extension/serv/category/group/delete.action", {ids: ids.join(",")}, 1000);
            });
        }

        //导出excel
        if(obj.event=='CUSTOM_EXPORT'){
            zmnExportUtil.doExportExcel('zmn-table','推广分类-分类关联');
        }
    });
    // 分类下拉框联动
    zmnPage.laySelectLinkage(form, "/common/categ/listChildsForShow.action", "categOneId", "categId", null);
    //对外暴露的接口
    exports('extension_serv_category_group_list_js', {});
});

var zmnProduct = {
    /**
     * 多级下拉框联动
     * @param form layui.form
     * @param url1 selectOneId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param url1 selectTwoId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param selectOneId 第1级select的ID，不含#号
     * @param selectTwoId 第2级select的ID，不含#号
     * @param selectThrId 第3级select的ID，不含#号，如无第3级则两级联动
     */
    laySelectSpecial: function (form, url1, selectOneId, selectTwoId) {
        let $ = layui.$;
        let extensionServCategId;
        form.on("select(" + selectOneId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            extensionServCategId = obj.value;
            $.ajax({
                type: "GET", url: url1,
                dataType: "json", data: {extensionServCategId: extensionServCategId},
                success: function (items) {
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    form.render("select");
                }
            });
        });
    },
}